/* let apiBaseUrl = "http://phpclub.org.cn:8080/api";
// 登录信息获取
let userOption = {};
// 1、首先判断地址是否带用户登录信息
if (location.search != '') {
	// 利用公共函数结构地址的数据
	userOption = getStrOfObj(location.search);
	// console.log(option);
}
// 2、地址不带登录信息，检查cookie是否存在
if (getCookie("jd")) {
	// 利用cookie获取用户信息
	let option = getStrOfObj(getCookie("jd"));
	// 地址对象+cookie对象
	for (let v in option) {
		if (!userOption.v) {
			userOption[v] = option[v]
		}
	}
}
console.log(userOption.id) */
// 读取地址列表
$.ajax({
	url:apiBaseUrl+"/address/lists",
	type:"get",
	dataType:"json",
	data:{userId:userOption.id},
	success:function(datas){
		//后端返回正常
		if(datas.msg != "ok"){
			layer.msg(datas.msg);
			return;
		}
		let da = datas.data;
		// 渲染地址html
		let reses = '';
		for (let v of da) {
			reses += `
				<!-- 单个地址item -->
				<div class="ress-item">
					<!-- 用户名标题和删除键 -->
					<div class="reitem-head">
						<h3>${v.consignee}</h3>
						${v.isDefault==1?"<span id='defress'>默认地址</span>":""}
						<div class="layui-btn-group">
							<button type="button" class="layui-btn layui-btn-primary layui-btn-sm addressid" data-addressId='${v.id}'>
								<i class="layui-icon">&#xe640;</i>
							</button>
						</div>
					</div>
					<!-- 用户地址信息 -->
					<div class="reitem-context">
						<!-- 左边 -->
						<div class="item-lcol">
							<!-- 收货人 -->
							<div class="item">
								<span class="label">收货人：</span>
								<div class="item-name iname">
									${v.consignee}
								</div>
							</div>
							<!-- 所在地区 -->
							<div class="item">
								<span class="label">所在地区：</span>
								<div class="item-name">
									<span class="iprovinceName">${v.provinceName}</span>
									<span class="icityName">${v.cityName}</span>
									<span class="iareaName">${v.areaName}</span>
								</div>
							</div>
							<!-- 地址 -->
							<div class="item">
								<span class="label">地址：</span>
								<div class="item-name">
									${v.provinceName}${v.cityName}${v.areaName}
									<span class="iaddress">${v.address}</span>
								</div>
							</div>
							<!-- 手机 -->
							<div class="item">
								<span class="label">手机：</span>
								<div class="item-name imobile">
									${v.mobile}
								</div>
							</div>
						</div>
						<!-- 右边 -->
						<div class="item-rcol">
							<!-- 收货人 -->
							<div class="item">
								<span class="label">支付方式：</span>
								<div class="item-name">
									货到付款
								</div>
							</div>
							<!-- 所在地区 -->
							<div class="item">
								<span class="label">配送方式：</span>
								<div class="item-name">
									第三方配送
								</div>
							</div>
						</div>
					</div>
					<!-- 编辑地址 -->
					<div class="extra">
						<a href="javascript:void(0)">设置默认</a>
						<a href="javascript:void(0)" class="edit" data-addressId="${v.addressId}">编辑</a>
					</div>
				</div>
			`;
		}
		// 添加地址
		$('.ress-list').html(reses);
	},
	error:function(e){
	}
})

// 修改地址
$('.ress-list').on("click",'.edit',function(e){
	// 获取父元素 -单个item对象
	let parent =  $(this).parent().parent();
	// 获取收货人
	let shName = parent.find('.iname').html().trim();
	// 获取手机号码
	let miblod = parent.find('.imobile').html().trim();
	// 获取省份
	let province =  parent.find('.iprovinceName').html().trim();
	// 获取城市
	let city =  parent.find('.icityName').html().trim();
	// 获取地区
	let region = parent.find('.iareaName').html().trim();
	// 获取详细地址
	let ress = parent.find('.iaddress').html().trim();
	//获取是否选中默认
	let chek = $('#defress') && 0;
	// 获取地址id
	let addressid = parent.find(".addressid").attr("data-addressid")
	// 添加弹出层
	layer.open({
	 type: 2, 
	 title:"修改地址",
	 content: `layaddress.html?shName=${shName}&miblod=${miblod}&province==${province}&city=${city}&region=${region}&ress=${ress}&chek=${chek}&btnName=立即修改&addressid=${addressid}`,
	 area:["700px","500px"]
	}); 
	
})

// 添加地址按键
$('#addres').on('click',function(){
	// 添加弹出层
	layer.open({
	 type: 2, 
	 title:"修改地址",
	 content: `layaddress.html`,
	 area:["700px","500px"]
	}); 
})

// 删除地址按键
$('.ress-list').on('click','.addressid',function(e){
	//保存按键对象
	let _this = $(this);
	// 获取地址id
	let addresId =  $(this).attr("data-addressid");
	console.log(addresId);
	// 发送删除请求
	$.ajax({
		url:apiBaseUrl+"/address/remove",
		type:"get",
		dataType:"json",
		data:{addressId:addresId ,userId:userOption.id},
		success:function(datas){
			//成功删除
			if(datas.msg == "ok"){
				_this.parent().parent().parent().remove()
			}
			layer.msg(datas.msg);
		}
	})
})